package com.liangzhong.modules.mail.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.ModulePanelTypeConst;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.MailEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.proxy.mail.MailProxy;
	import com.lz.football.module.mail.IMailView;
	
	import flash.display.DisplayObject;
	
	import lz.controls.Alert;
	import lz.controls.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;
	
	/**
	 *邮件 
	 * @author Administrator
	 * 
	 */	
	public class MailMediator extends ModuleMediator
	{
		public static const MediatorName :String = "MailMediator";
		
		public function MailMediator(mediatorName:String=null, viewComponent:Object=null,param:Object = null)
		{
			super(mediatorName, viewComponent,param);
		}
		/**
		 *获取视图 
		 * @return 
		 * 
		 */		
		public function get view():IMailView
		{
			return viewComponent as IMailView;
		}
		/**
		 * 初始化
		 * 
		 */		
		override public function initMediator():void
		{
			addEventListeners();
		}
		/**
		 * 
		 * 添加事件
		 */		
		override public function addEventListeners():void
		{
			(view as DisplayObject).addEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			(view as DisplayObject).addEventListener(MailEvent.GETGOODS,getGoodsHandler);
		}
		/**
		 * 删除事件
		 * 
		 */		
		override public function removeEventListeners():void
		{
			(view as DisplayObject).removeEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			(view as DisplayObject).removeEventListener(MailEvent.GETGOODS,getGoodsHandler);
		}
		/**
		 * 删除模块
		 * @param event
		 * 
		 */
		private function onRemoveModuleHandler(e:ModuleEvent):void
		{
			removeMediator();
		}
		/**
		 *获取物品 
		 * @param e
		 * 
		 */		
		private function getGoodsHandler(e :MailEvent):void
		{
			this.sendNotification(ApplicationConstants.MAIL_GET_GOODS,e.param);
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			super.removeMediator();
			facade.removeMediator(MediatorName);
		}
		/**
		 * 捕获
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification :INotification):void
		{
			super.handleNotification(notification);
			switch(notification.getName())
			{
				
			}
			
		}
		/**
		 * 监听
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [];
		}
		/**
		 * 注册
		 * 
		 */		
		override public function onRegister():void
		{
			if(ResourceManager.instance.isContainResource(ResourceConst.MAIL_RESOURCE))
			{
				showUI();
				
			}else
			{
				loader = new QueueLoader();
				var config:XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).mail[0];
				bytesTotal = int(config.@totalBytes);
				var releaseVersion :String;
				for each(var item:XML in config.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				
				loadRes();	
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			super.onLoadComplete(e);
			showUI();
			ResourceManager.instance.putResource(ResourceConst.MAIL_RESOURCE,resList);
		}
		/**
		 * 显示界面 
		 * 
		 */
		private function showUI():void
		{
			viewComponent = Reflection.createInstance("com.lz.football.module.mail.view.MailView");
			view.initUI();
			PopUpManager.addPopUp(view as DisplayObject,GlobalDef.GameWindowContainer,GlobalDef.GameStage.stageWidth,GlobalDef.GameStage.stageWidth,false);
			addEventListeners();
			//设置缓存位图
			setCacheAsBitmap(true);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			view.destroyView();
		}
	}
}